/**
 * 
 */
package com.nari.interfaces.decipher.common;

import java.math.BigDecimal;

/**
 * 热量表数据解析器
 * @author Xiaopeng
 * @date 2014-3-29
 */
public class HeatMeterDataProtocolDecipher {

	/**
	 * 解析热量表的结算日热量数据
	 * 如：78 56 34 12表示12345678
	 * @param settleAccountHeatCode
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-29
	 */
	public static double decodeSettleAccountHeat(String settleAccountHeatCode) {
		double settleAccountHeat = 0;
		if(settleAccountHeatCode != null) {
			settleAccountHeatCode = settleAccountHeatCode.trim().replaceAll(" ", "");
			if(settleAccountHeatCode.length() == 8) {
				//高两位
				String high2Value = settleAccountHeatCode.substring(6, 8);
				//中两位
				String middle2Value = settleAccountHeatCode.substring(4, 6);
				//低两位
				String low2Value = settleAccountHeatCode.substring(2, 4);
				//精度位
				String precisionValue = settleAccountHeatCode.substring(0, 2);
				//重建结算日热量
				settleAccountHeatCode = high2Value + middle2Value + low2Value + precisionValue;
				settleAccountHeat = new BigDecimal(settleAccountHeatCode).doubleValue();
			}
		}
		return settleAccountHeat;
	}
	
	/**
	 * 解析热量表的当前热量或累计热量数据
	 * 如：56 34 12 00表示123456
	 * @param sumHeatCode
	 * @return
	 * @author Xiaopeng
	 * @date 2014-3-29
	 */
	public static double decodeSumHeat(String sumHeatCode) {
		double sumHeat = 0;
		if(sumHeatCode != null) {
			sumHeatCode = sumHeatCode.trim().replaceAll(" ", "");
			if(sumHeatCode.length() == 8) {
				//高两位
				String high2Value = sumHeatCode.substring(6, 8);
				//中两位
				String middle2Value = sumHeatCode.substring(4, 6);
				//低两位
				String low2Value = sumHeatCode.substring(2, 4);
				//精度位
				String precisionValue = sumHeatCode.substring(0, 2);
				//重建累计热量
				sumHeatCode = high2Value + middle2Value + low2Value + precisionValue;
				sumHeat = new BigDecimal(sumHeatCode).doubleValue();
			}
		}
		return sumHeat;
	}
	
	/**
	 * 解析热量表的热功率
	 * 如：78 56 34 12表示12345678
	 * @param heatPowerCode
	 * @return
	 * @author Xiaopeng
	 * @date 2014-3-29
	 */
	public static double decodeHeatPower(String heatPowerCode) {
		double heatPower = 0;
		if(heatPowerCode != null) {
			heatPowerCode = heatPowerCode.trim().replaceAll(" ", "");
			if(heatPowerCode.length() == 8) {
				//高两位
				String high2Value = heatPowerCode.substring(6, 8);
				//中两位
				String middle2Value = heatPowerCode.substring(4, 6);
				//低两位
				String low2Value = heatPowerCode.substring(2, 4);
				//精度位
				String precisionValue = heatPowerCode.substring(0, 2);
				//重建热功率
				heatPowerCode = high2Value + middle2Value + low2Value + precisionValue;
				heatPower = new BigDecimal(heatPowerCode).doubleValue();
			}
		}
		return heatPower;
	}
	
	/**
	 * 解析热量表的瞬时流量
	 * 如：56 34 12 00表示123456
	 * @param instantFlowCode
	 * @author Xiaopeng
	 * @date 2014-3-29
	 */
	public static double decodeInstantFlow(String instantFlowCode) {
		double instantFlow = 0;
		if(instantFlowCode != null) {
			instantFlowCode = instantFlowCode.trim().replaceAll(" ", "");
			if(instantFlowCode.length() == 8) {
				//高两位
				String high2Value = instantFlowCode.substring(6, 8);
				//中两位
				String middle2Value = instantFlowCode.substring(4, 6);
				//低两位
				String low2Value = instantFlowCode.substring(2, 4);
				//精度位
				String precisionValue = instantFlowCode.substring(0, 2);
				//重建瞬时流量
				instantFlowCode = high2Value + middle2Value + low2Value + precisionValue;
				instantFlow = new BigDecimal(instantFlowCode).doubleValue();
			}
		}
		return instantFlow;
	}
	
	/**
	 * 解析热量表的累计流量
	 * 如：78 56 34 12表示12345678
	 * @param sumFlowCode
	 * @return
	 * @author Xiaopeng
	 * @date 2014-3-29
	 */
	public static double decodeSumFlow(String sumFlowCode) {
		double sumFlow = 0;
		if(sumFlowCode != null) {
			sumFlowCode = sumFlowCode.trim().replaceAll(" ", "");
			if(sumFlowCode.length() == 8) {
				//高两位
				String high2Value = sumFlowCode.substring(6, 8);
				//中两位
				String middle2Value = sumFlowCode.substring(4, 6);
				//低两位
				String low2Value = sumFlowCode.substring(2, 4);
				//精度位
				String precisionValue = sumFlowCode.substring(0, 2);
				//重建累计流量
				sumFlowCode = high2Value + middle2Value + low2Value + precisionValue;
				sumFlow = new BigDecimal(sumFlowCode).doubleValue();
			}
		}
		return sumFlow;
	}
	
	/**
	 * 解析热量表的供水温度
	 * 如：01 53表示5301（位数与协议文档有出入）
	 * @param supplyTemperatureCode
	 * @return
	 * @author Xiaopeng
	 * @date 2014-3-29
	 */
	public static double decodeSupplyTemperature(String supplyTemperatureCode) {
		double supplyTemperature = 0;
		if(supplyTemperatureCode != null) {
			supplyTemperatureCode = supplyTemperatureCode.trim().replaceAll(" ", "");
			if(supplyTemperatureCode.length() == 4) {
				//整数两位
				String integer2Value = supplyTemperatureCode.substring(2, 4);
				//精度位
				String precisionValue = supplyTemperatureCode.substring(0, 2);
				//重建供水温度
				supplyTemperatureCode = integer2Value + precisionValue;
				supplyTemperature = new BigDecimal(supplyTemperatureCode).doubleValue();
			}
		}
		return supplyTemperature;
	}
	
	/**
	 * 解析热量表的回水温度
	 * 如：05 50表示5005（位数与协议文档有出入）
	 * @param backTemperatureCode
	 * @return
	 * @author Xiaopeng
	 * @date 2014-3-29
	 */
	public static double decodeBackTemperature(String backTemperatureCode) {
		double backTemperature = 0;
		if(backTemperatureCode != null) {
			backTemperatureCode = backTemperatureCode.trim().replaceAll(" ", "");
			if(backTemperatureCode.length() == 4) {
				//整数两位
				String integer2Value = backTemperatureCode.substring(2, 4);
				//精度位
				String precisionValue = backTemperatureCode.substring(0, 2);
				//重建回水温度
				backTemperatureCode = integer2Value + precisionValue;
				backTemperature = new BigDecimal(backTemperatureCode).doubleValue();
			}
		}
		return backTemperature;
	}
	
	/**
	 * 解析热量表的累计运行时间
	 * 如：56 34 12表示123456
	 * @param runHoursCode
	 * @return
	 * @author Xiaopeng
	 * @date 2014-3-29
	 */
	public static double decodeRunHours(String runHoursCode) {
		double runHours = 0;
		if(runHoursCode != null) {
			runHoursCode = runHoursCode.trim().replaceAll(" ", "");
			if(runHoursCode.length() == 6) {
				//高两位
				String high2Value = runHoursCode.substring(4, 6);
				//中两位
				String middle2Value = runHoursCode.substring(2, 4);
				//低两位
				String low2Value = runHoursCode.substring(0, 2);
				runHoursCode = high2Value + middle2Value + low2Value;
				//重建累计运行时间
				runHours = new BigDecimal(runHoursCode).doubleValue();
			}
		}
		return runHours;
	}
	
	/**
	 * 根据热量表抄表数据的状态字解析出其型号编号信息
	 * 说明：热量表数据原始解析信息中状态字是四个字符的的BCD码[S3S2 S1S0]
	 * 其中S3S2表示热量表抄收数据的精度类型，S0表示报错信息
	 * @param statusWords
	 * @return
	 * @author Xiaopeng
	 * @date 2014-6-24
	 */
	public static String decipherTypeCode(String statusWords) {
		return new Integer(statusWords.substring(0, 2)).toString();
	}
	
	/**
	 * 根据热量表抄表数据的状态字解析出其报错信息编号
	 * 说明：热量表数据原始解析信息中状态字是四个字符的的BCD码[S3S2 S1S0]
	 * 其中S3S2表示热量表抄收数据的精度类型，S0表示报错信息
	 * @param statusWords
	 * @return
	 * @author Xiaopeng
	 * @date 2014-12-4
	 */
	public static String decipherWrongCode(String statusWords) {
		String wrongCode = String.valueOf(statusWords.charAt(3));
		return wrongCode;
	}
}
